
find_library(CMOCKA_LIBRARY NAMES cmocka)
add_library(cmocka SHARED IMPORTED tests/config/test_oop.c tests/config/test_sys.c tests/ds/test_graph.c tests/ds/test_stack.c tests/engine/test_aopengine.c tests/engine/test_oopengine.c tests/engine/test_fpengine.c tests/premise/test_lexer.c tests/engine/test_loaderengine.c tests/engine/test_logicengine.c tests/engine/test_pmengine.c tests/engine/test_refactorengine.c tests/premise/test_component.c tests/engine/test_lexerengine.c)
set_property(TARGET cmocka PROPERTY IMPORTED_LOCATION "${CMOCKA_LIBRARY}")

#
# https://stackoverflow.com/questions/14306642/adding-multiple-executables-in-cmake
#
# add_test_suite(SUITE check_ds RUNNER runner_ds TARGET ds SOURCES ${TEST_DS_SRCS})
#
include(CMakeParseArguments)
function(add_test_suite)
    cmake_parse_arguments(TEST "" "SUITE;TARGET" "RUNNER;SOURCES" ${ARGN})
    set(TARGETS)
    foreach( testsourcefile ${TEST_SOURCES} )
        string( REPLACE ".c" "" testname ${testsourcefile} )
        add_executable( ${testname} ${testsourcefile} ${TEST_RUNNER} )
        add_test(${testname} ${testname})
        target_link_libraries( ${testname} cmocka engine ${TEST_TARGET} )
        list(APPEND TARGETS ${testname})
    endforeach( testsourcefile ${_test_sources} )
    add_custom_target(${TEST_SUITE} COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS ${TARGETS})
endfunction(add_test_suite)

#
# add_cmocka_stubs(test_init fopen)
#
function(add_cmocka_stubs _args)
    if(CMAKE_COMPILER_IS_GNUCC)
        set(new_link_flags "")
        set(_target ${ARGV0})
        set(INDEX 1)
        while(INDEX LESS ${ARGC})
            set(new_link_flags "${new_link_flags} -Wl,--wrap=${ARGV${INDEX}}")
            math(EXPR INDEX "${INDEX} + 1")
        endwhile()
        get_target_property(existing_link_flags ${_target} LINK_FLAGS)
        if(existing_link_flags)
            set(new_link_flags "${existing_link_flags} ${new_link_flags}")
        endif()
        #message(WARNING "${new_link_flags} ${LIBRARY_OUTPUT_PATH}")
        set_target_properties(${_target} PROPERTIES LINK_FLAGS ${new_link_flags})
    endif()
endfunction()

